/**
 * 项目名称:定义方法统计集合中指定元素出现的次数
 *
 * @Author: 夏国倩
 * @Date: 2023/9/21 11:46
 * @Copyright
 */
package List;
import java.util.ArrayList;
import java.util.List;
public class Test1 {
    public static void main(String[] args) {
        List<String> list=new ArrayList<>();
        list.add("a");
        list.add("a");
        list.add("a");
        list.add("b");
        list.add("b");
        list.add("c");
        list.add("d");
        list.add("d");
        list.add("d");
        list.add("d");
        list.add("d");


        System.out.println(frequency(list,"a")); //3
        System.out.println(frequency(list,"b")); //2
        System.out.println(frequency(list,"c")); //1
        System.out.println(frequency(list,"d")); //5
        System.out.println(frequency(list,"xxx")); //0
    }
    public static int frequency(List<String> list,String string){
        int count=0;
        for (String s:list) {
            if (string.equals(s)){
                count++;
            }
        }
        return count;
    }
}
